﻿namespace WcfJsHoster
{
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Runtime.CompilerServices;
    using System.Xml;

    public class APISummary
    {
        private static Dictionary<string, AssemblyInfo> AllSummary = new Dictionary<string, AssemblyInfo>();

        public static void Clear()
        {
            AllSummary.Clear();
        }

        public static List<string> GetClassSummary(string Assembly, string className)
        {
            foreach (string str in AllSummary.Keys)
            {
                if (str == Assembly)
                {
                    foreach (Member member in AllSummary[str].Members)
                    {
                        if (member.Name == string.Format("T:{0}", className))
                        {
                            return member.Mark;
                        }
                    }
                }
            }
            return new List<string>();
        }

        public static List<string> GetMethodSummary(string Assembly, string methodName)
        {
            foreach (string str in AllSummary.Keys)
            {
                if (str == Assembly)
                {
                    foreach (Member member in AllSummary[str].Members)
                    {
                        if (member.Name == string.Format("M:{0}", methodName))
                        {
                            return member.Mark;
                        }
                    }
                }
            }
            return new List<string>();
        }

        public static void LoadAllSummaryInfo(string dllPath)
        {
            FileInfo[] files = new DirectoryInfo(dllPath).GetFiles("*.dll");
            if (files.Length != 0)
            {
                foreach (FileInfo info2 in files)
                {
                    string fullName = info2.FullName;
                    string path = fullName.Substring(0, fullName.Length - 4) + ".xml";
                    if (File.Exists(path))
                    {
                        XmlDocument document = new XmlDocument();
                        try
                        {
                            document.Load(path);
                            System.Xml.XmlNode node = document.SelectSingleNode("doc");
                            System.Xml.XmlNode firstChild = node.FirstChild;
                            System.Xml.XmlNode lastChild = node.LastChild;
                            AssemblyInfo info3 = new AssemblyInfo {
                                AssemblyName = firstChild.FirstChild.InnerText
                            };
                            foreach (System.Xml.XmlNode node4 in lastChild.ChildNodes)
                            {
                                try
                                {
                                    XmlElement element = (XmlElement) node4;
                                    Member item = new Member {
                                        Name = element.GetAttribute("name")
                                    };
                                    foreach (System.Xml.XmlNode node5 in node4.ChildNodes)
                                    {
                                        XmlElement element2 = (XmlElement) node5;
                                        if (element2.Name == "summary")
                                        {
                                            item.Mark.Add(element2.InnerText.Trim());
                                        }
                                        else if (element2.Name == "param")
                                        {
                                            item.Mark.Add(string.Format("{0}:{1}", element2.GetAttribute("name"), element2.InnerText.Trim()));
                                        }
                                        else
                                        {
                                            continue;
                                        }
                                    }
                                    info3.Members.Add(item);
                                }
                                catch
                                {
                                }
                            }
                            AllSummary.Add(info3.AssemblyName, info3);
                        }
                        catch
                        {
                        }
                    }
                }
            }
        }

        private class AssemblyInfo
        {
            private List<APISummary.Member> members = new List<APISummary.Member>();

            public string AssemblyName { get; set; }

            public List<APISummary.Member> Members
            {
                get
                {
                    return this.members;
                }
                set
                {
                    this.members = value;
                }
            }
        }

        private class Member
        {
            private List<string> mark = new List<string>();
            private string name = "";

            public List<string> Mark
            {
                get
                {
                    return this.mark;
                }
                set
                {
                    this.mark = value;
                }
            }

            public string Name
            {
                get
                {
                    return this.name;
                }
                set
                {
                    this.name = value;
                }
            }
        }
    }
}

